from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from home import models,serhome
from course.models import Direction, Course,Cate
from course.sercourse import DirectionSerializer, CourseSerializer
# Create your views here.

class TopNav(APIView):
    def get(self, request):
        top_nav = models.NavModel.objects.filter(isdel=False,location=1).order_by('sort')
        ser=serhome.NavSerializer(top_nav,many=True)
        return Response({'status':200,'msg':'ok','data':ser.data})

class Footer(APIView):
    def get(self, request):
        footer = models.NavModel.objects.filter(isdel=False,location=0).order_by('sort')
        ser=serhome.NavSerializer(footer,many=True)
        return Response({'status':200,'msg':'ok','data':ser.data})

class Banner(APIView):
    def get(self, request):
        banner = models.BannerModel.objects.filter(isdel=False)
        ser=serhome.BannerSerializer(banner,many=True)
        return Response({'status':200,'msg':'ok','data':ser.data})


class Category(APIView):
    def get(self, request):
        category = Direction.objects.filter(isdel=False)
        ser=DirectionSerializer(category,many=True)
        return Response({'status':200,'msg':'ok','data':ser.data})

class CourseView(APIView):
    def get(self, request,did):
        if did == '0':
            course = Course.objects.all()
            ser = CourseSerializer(course, many=True)
        else:
            course = Course.objects.filter(isdel=False,cate__direction_id=did)
            ser=CourseSerializer(course,many=True)
        return Response({'status':200,'msg':'ok','data':ser.data})


class Nowcate(APIView):
    def get(self, request):
        nowcate = Direction.objects.filter(isdel=False,id=request.query_params.get('id')).first()
        ser=DirectionSerializer(nowcate)
        allcate=Direction.objects.filter(isdel=False,parent=request.query_params.get('id'))
        catelist=[]
        for i in allcate:
            catelist.append(i.id)
        allcourse = Course.objects.filter(isdel=False,cate__direction_id__in=catelist,ishome=True).order_by('order')
        ser2=CourseSerializer(allcourse,many=True)
        mydata=ser.data
        mydata['course']=ser2.data[:4]
        return Response({'code':200,'msg':'ok','data':mydata})